﻿using System;
using System.Collections.Generic;
using System.Text;
using TSVNAddin.Config;
using System.Windows.Forms;

namespace TSVNAddin.Core
{
	public class AboutAddinCommand : ICommand
	{
		IDialogFactory _Factory;
		ConfigStorage _ConfigStorage;

		public AboutAddinCommand(IDialogFactory factory, ConfigStorage configStorage)
		{
			_Factory = factory;
			_ConfigStorage = configStorage;
		}

		public void Execute()
		{
			var config = _ConfigStorage.LoadAddinConfig();
			ShowDialog(config);
		}

		public CommandStatus QueryStatus()
		{
			return CommandStatus.Enabled;
		}

		public void ShowAboutDialogOnStart()
		{
			var config = _ConfigStorage.LoadAddinConfig();
			if (config.AboutOnStart == true)
				ShowDialog(config);
		}

		private void ShowDialog(AddinConfig config)
		{
			using (var dlg = _Factory.CreateAboutDialog())
			{
				dlg.ShowOnStart = config.AboutOnStart;

				if (dlg.ShowDialog() != DialogResult.OK)
					return;

				config.AboutOnStart = dlg.ShowOnStart;
			}

			_ConfigStorage.StoreAddinConfig(config);
		}
	}
}
